﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using BusinessPlatform.Core.EntityParser;
using BusinessPlatform.Core.Result;
using BusinessPlatform.Test.SalesOrder;
using BusinessPlatform.Core.Criteria;
using System.Threading;
using System.Windows.Forms;
using BusinessPlatform.Test.Users;
using BusinessPlatform.Test.BusinessPartner;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BusinessPlatform.Test.Binding;


namespace BusinessPlatform.Test
{
    delegate void CurrentThreadStatus(bool Status);
    /// <summary>
    /// SalesOrderTest 的摘要说明
    /// </summary>
    [TestClass]
    public class OrderTest
    {
        public OrderTest()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，该上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        //
        // 编写测试时，可以使用以下附加特性:
        //
        // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // 在运行每个测试之前，使用 TestInitialize 来运行代码
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // 在每个测试运行完之后，使用 TestCleanup 来运行代码
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        /// <summary>
        /// 添加订单
        /// </summary>
        [TestMethod]
        public void SalesOrderNewTest()
        {
            EntityParser ep = new EntityParser();
            ISalesOrder order;
            ISalesOrderLine line;
            IResult result;
            #region
            order = new SalesOrder.SalesOrder();
            order.CardCode = "C001";
            order.CardName = "联想集团（中国）有限公司";
            order.DocDate = DateTime.Now;
            order.ObjectType = "SalesOrder";
            order.IsPartDelivery = Data.Enumerator.BOYesOrNo.Yes;
            order.Remarks = Thread.CurrentThread.Name;

            line = order.SalesOrderLines.New();
            line.ItemCode = "T410";
            line.ItemName = "电脑";
            line.Quantity = 10;
            line.Price = 8000;
            line.LineTotal = line.Quantity * line.Price;

            line = order.SalesOrderLines.New();
            line.ItemCode = "T420";
            line.ItemName = "电脑";
            line.Quantity = 15;
            line.Price = 8000;
            line.LineTotal = line.Quantity * line.Price;

            order.DocTotal = 180000;
            #endregion
            result = ep.Save<ISalesOrder>(order);
            Assert.IsTrue(result.ResultFlag);
        }
        #region 多线程
        private void NewTen()
        {
            for (int i = 1; i <= 10; i++)
            {
                SalesOrderNewTest();
                Thread.Sleep(1000);
            }
        }
        private void NewTenThread()
        {
            Thread mythrad;
            for (int i = 1; i <= 10; i++)
            {
                mythrad = new Thread(new ThreadStart(NewTen));
                mythrad.Name = "线程:" + i.ToString();
                mythrad.Start();
            }
        }
        [TestMethod]
        public void SalesOrderThreadNewTest()
        {
            NewTenThread();
            Thread.Sleep(100000);
        }
        #endregion
        [TestMethod]
        public void SalesOrderSearchTest()
        {
            ICriteria cri = new Criteria();
            var con = cri.Conditions.New();
            con.FieldName = "DocEntry";
            con.Operation = EnumOperateSymbol.EQUAL;
            con.ConditionValue = "1";

            EntityParser ep = new EntityParser();
            var result = ep.Search<ISalesOrder>(cri);

            Assert.IsTrue(result.ResultFlag);
        }
        [TestMethod]
        public void SalesOrderUpdateTest()
        {
            ICriteria cri = new Criteria();
            var con = cri.Conditions.New();
            con.FieldName = "DocEntry";
            con.Operation = EnumOperateSymbol.EQUAL;
            con.ConditionValue = "1";
            EntityParser ep = new EntityParser();
            var result = ep.Search<ISalesOrder>(cri);
            Assert.IsTrue(result.ResultFlag);

            foreach (var item in result.BusinessObject)
            {
                SalesOrder.ISalesOrder order = item;
                order.Remarks = "第一次修改";
                order.UpdateDate = DateTime.Now;
                order.Updator = 1;
                foreach (var line in order.SalesOrderLines)
                {
                    line.Remarks = "已修改：" + DateTime.Now.ToString("yyyy-mm-dd hh:MM:ss");
                    line.UpdateDate = DateTime.Now;
                    line.Updator = 1;
                }
                var resultsave = ep.Save(order);
                Assert.IsTrue(resultsave.ResultFlag);
            }
        }
        [TestMethod]
        public void SalesOrderDeleteTest()
        {

            ICriteria cri = new Criteria();
            var con = cri.Conditions.New();
            con.FieldName = "DocEntry";
            con.Operation = EnumOperateSymbol.EQUAL;
            con.ConditionValue = "1";
            EntityParser ep = new EntityParser();
            var result = ep.Search<ISalesOrder>(cri);
            Assert.IsTrue(result.ResultFlag);
            foreach (var item in result.BusinessObject)
            {
                SalesOrder.ISalesOrder order = item;
                foreach (var line in order.SalesOrderLines)
                {
                    if (line.LineNumber == 2)
                        line.Delete();
                }
                var resultsave = ep.Save(order);
                Assert.IsTrue(resultsave.ResultFlag);
            }
        }
        [TestMethod]
        public void BindingTest()
        {

            ICriteria cri = new Criteria();
            var con = cri.Conditions.New();
            con.FieldName = "DocEntry";
            con.Operation = EnumOperateSymbol.EQUAL;
            con.ConditionValue = "1";

            EntityParser ep = new EntityParser();
            var result = ep.Search<ISalesOrder>(cri);
            if (result.ResultFlag)
            {
                var order = result.BusinessObject[0];
                var binding = new Binding.SalesOrderBindingTest();
                binding.BindUI(order);
                binding.Show();
                Application.Run();
            }

        }
        [TestMethod]
        public void ListBindingTest()
        {
            ICriteria cri = new Criteria();
            var con = cri.Conditions.New();
            con.FieldName = "DocEntry";
            con.Operation = EnumOperateSymbol.GRATER_THAN;
            con.ConditionValue = "0";

            EntityParser ep = new EntityParser();
            var result = ep.Search<ISalesOrder>(cri);

            if (result.ResultFlag)
            {
                var listbinding = new Binding.SalesOrderListBindingTest();
                listbinding.BindUI(result.BusinessObject);
                listbinding.Show();
                Application.Run();
            }
        }
        [TestMethod]
        public void UsersTest()
        {
            IUsers user = new Users.Users();
            user.UserCode = "manager";
            user.UserName = "manager";
            EntityParser ep = new EntityParser();
            var result = ep.Save<IUsers>(user);
            Assert.IsTrue(result.ResultFlag);

        }

        public void BusinessPartnerTest()
        {
            IBusinessPartner bp = new BusinessPartner.BusinessPartner();
        }
        [TestMethod]
        public void WinFormSalesOrderTest()
        {
            ICriteria cri = new Criteria();
            var con = cri.Conditions.New();
            con.FieldName = "DocEntry";
            con.Operation = EnumOperateSymbol.EQUAL;
            con.ConditionValue = "1";

            EntityParser ep = new EntityParser();
            var result = ep.Search<ISalesOrder>(cri);
            if (result.ResultFlag)
            {
                var order = result.BusinessObject[0];
                var binding = new SalesOrderWinForm();
                binding.BindUI(order);
                binding.Show();
                Application.Run();
            }
        }
    }
}
